package dk.mitaffald.main;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import dk.tankegang.mitaffald.R;


import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class CatAbc extends ListActivity{

	// JSON Node names
    private static final String TAG_id = "fraction_id";
    private static final String TAG_name = "fraction_name";
    private static final String TAG_type = "fraction_type";
    private static final String TAG_typetext = "fraction_typetext";
    private static final String TAG_desc = "fraction_description";
    private static final String TAG_extra = "fraction_extra";
    private static final String TAG_company_id = "company_id";
    private static final String TAG_lang = "fraction_language";

    private static final String TAG = "Debug of Project"; // 

     @Override
     public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
	    String url = "http://webservice.mitaffald.dk/webservice/getFractionList.php?company_id=0";
 
	    Log.d(TAG, "Leyth URL =" + url ); 
	    
	    
		// Disabled = ingen support for 4.0
		// Enabled = support for 4.0
	    // status = enabled
	    // for remove delete if statement
	    if (android.os.Build.VERSION.SDK_INT > 9) {
		      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		      StrictMode.setThreadPolicy(policy);
		 }
		 
		ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
		JSONParser jParser = new JSONParser();
     	JSONArray json = jParser.getJSONFromUrl(url);
     	try {
			   for(int i = 0; i < json.length(); i++){
			        JSONObject c = json.getJSONObject(i);
		        
				String fraction_id = c.getString(TAG_id);
				String fraction_name = c.getString(TAG_name);
				String fraction_type = c.getString(TAG_type);
				String fraction_typetext = c.getString(TAG_typetext);
				String fraction_description = c.getString(TAG_desc);	
				String fraction_extra = c.getString(TAG_extra);
				String company_id = c.getString(TAG_company_id);
				String fraction_language = c.getString(TAG_lang);
				
			// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();
				
			// adding each child node to HashMap key => value
				map.put(TAG_id, fraction_id);
				map.put(TAG_name, fraction_name);
				map.put(TAG_type, fraction_type);
				map.put(TAG_typetext, fraction_typetext);
				map.put(TAG_desc, fraction_description);
				map.put(TAG_extra, fraction_extra);
				map.put(TAG_company_id, company_id);
				map.put(TAG_lang, fraction_language);
				
					// adding HashList to ArrayList
				contactList.add(map);
			   }
		} catch (JSONException e) {
		    e.printStackTrace();
			Log.e("JSON Parser", "Error parsing data " + e.toString());
			
		}
		/**
		 * Updating parsed JSON data into ListView
		 * */
		ListAdapter adapter = new SimpleAdapter(this, contactList,
				R.layout.list_item,
				new String[] { TAG_name, TAG_type, TAG_desc }, new int[] {
						R.id.name, R.id.email, R.id.mobile });

		setListAdapter(adapter);

		// selecting single ListView item
		ListView lv = getListView();

		// Launching new screen on Selecting Single ListItem
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// getting values from selected ListItem
				String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
				String cost = ((TextView) view.findViewById(R.id.email)).getText().toString();
				String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
				
				// Starting new intent
				Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
				in.putExtra(TAG_name, name);
				in.putExtra(TAG_type, cost);
				in.putExtra(TAG_desc, description);

				startActivity(in);

			}
		});
}

	 
	}